package com.yizisu.basemvvm.view

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

/**
 * 若红楼梦空，亦初心不变
 * 作者：thinker
 * 包名：android_bull_b.bull.member.thinker.com.androidbullb.utils
 * 时间：2018/12/28 18:02
 * 描述：P：item类型---t:viewHolder
 */
abstract class BaseMutableRcvAdapter<P> : BaseRcvAdapter<P, RecyclerView.ViewHolder>() {
    private val viewTypeAndLayoutResMap = mutableMapOf<Int/*viewType*/, Int/*layoutRes*/>()
    /**
     * 设置点击事件
     */
    private var itemClick: ((itemView: View, position: Int, itemData: P) -> Unit)? = null
    /**
     * 设置点击事件
     */
    private var itemClickId: Int? = null

    /**
     * recyclerView的item的res
     * 为了支持多类型的item
     */
    abstract fun getMutableItemInfo(position: Int): MutableItemInfo

    abstract fun onCreateViewHolder(itemView: View, viewType: Int): RecyclerView.ViewHolder

    final override fun getViewType(position: Int): Int {
        val rcvLayoutRes = getMutableItemInfo(position)
        viewTypeAndLayoutResMap[rcvLayoutRes.viewType] = rcvLayoutRes.itemLayoutRes
        return rcvLayoutRes.viewType
    }

    /**
     * 创建viewHolder
     */
    final override fun onCreateHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val layoutRes = viewTypeAndLayoutResMap[viewType]
                ?: throw IllegalArgumentException("getMutableItemInfo返回了null数据")
        return onCreateViewHolder(LayoutInflater.from(parent.context).inflate(layoutRes, parent, false), viewType)
    }

    final override fun getItemLayoutRes(): Int {
        return 0
    }

    final override fun getItemLayoutView(inflater: LayoutInflater, parent: ViewGroup, viewType: Int): View? {
        return super.getItemLayoutView(inflater, parent, viewType)
    }

    final override fun getItemLayoutRes(inflater: LayoutInflater, parent: ViewGroup, viewType: Int): Int? {
        return super.getItemLayoutRes(inflater, parent, viewType)
    }

    /**
     * 这个方法永远不会调用
     */
    final override fun onCreateViewHolder(itemView: View): RecyclerView.ViewHolder {
        return object : RecyclerView.ViewHolder(itemView) {

        }
    }

    /**
     * 数据类型
     */
    data class MutableItemInfo(
            var viewType: Int,
            var itemLayoutRes: Int/*view或者layoutRes*/
    )
}





